package com.hs.shirodemo2.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hs.shirodemo2.entity.Student;
import com.hs.shirodemo2.mapper.StudentMapper;
import com.hs.shirodemo2.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    /*
    * 自动注入Mapper接口的代理对象：因为在Spring的配置文件里面进行了配置
    * */
    @Autowired
    private StudentMapper studentMapper;

    @Override
    public void add(Student student) {
        studentMapper.insert(student);
    }

    @Override
    public void edit(Student student) {
        studentMapper.update(student);
    }

    @Override
    public void remove(Integer id) {
        studentMapper.delete(id);
    }

    @Override
    public void removeBatch(Integer[] ids) {
        studentMapper.deleteBatch(ids);
    }

    @Override
    public List<Student> find(Student student) {
        return studentMapper.select(student);
    }

    @Override
    public PageInfo findPage(Integer pageNum, Integer pageSize, Student student) {
        // 开启分页
        PageHelper.startPage(pageNum, pageSize);

        List<Student> list = studentMapper.select(student);

        // 分页信息对象
        PageInfo<Student> pageInfo = new PageInfo<>(list);

        return pageInfo;
    }

    @Override
    public Student findById(Integer id) {
        return studentMapper.selectById(id);
    }
}